package com.eastmoney.emis.edda.domain.entity;

import com.eastmoney.emis.edda.domain.valueobject.ServiceTimeType;
import com.eastmoney.emis.edda.domain.valueobject.ServiceType;
import com.eastmoney.emis.edda.infrastructure.constraint.po.EddaTimeConstraintPO;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.domain.entity
 * @author: 汪程程
 * @date: 2022/10/15 9:45 AM
 * @version: 1.0
 */
public class ServiceTime {


    private ServiceType serviceType;

    private boolean enable;
    private LocalTime amOpen;
    private LocalTime amClose;
    private LocalTime pmOpen;
    private LocalTime pmClose;
    private ServiceTimeType serviceTimeType;

    public ServiceTime() {
    }

    public ServiceTime(EddaTimeConstraintPO po) {
        this.serviceType = ServiceType.parse(po.SERVICETYPE);
        this.serviceTimeType = ServiceTimeType.parse(po.SERVICETIMETYPE);
        this.enable = po.STATUS.equals(1);
        this.amOpen = timeFormat(fillLeadingZero(po.AMOPEN));
        this.amClose = timeFormat(fillLeadingZero(po.AMCLOSE));
        this.pmOpen = timeFormat(fillLeadingZero(po.PMOPEN));
        this.pmClose = timeFormat(fillLeadingZero(po.PMCLOSE));
    }

    private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("HHmmss");

    private LocalTime timeFormat(String timeStr) {
        return LocalTime.parse(timeStr, df);
    }

    private String fillLeadingZero(String timeStr) {
        return String.format("%6s", timeStr).replace(" ", "0");
    }

    public ServiceType getServiceType() {
        return serviceType;
    }

    public void setServiceType(ServiceType serviceType) {
        this.serviceType = serviceType;
    }

    public boolean enable() {
        return enable;
    }

    public boolean serving() {
        return this.serviceTimeType.test(this);
    }

    public LocalTime getAmOpen() {
        return amOpen;
    }

    public void setAmOpen(LocalTime amOpen) {
        this.amOpen = amOpen;
    }

    public LocalTime getAmClose() {
        return amClose;
    }

    public void setAmClose(LocalTime amClose) {
        this.amClose = amClose;
    }

    public LocalTime getPmOpen() {
        return pmOpen;
    }

    public void setPmOpen(LocalTime pmOpen) {
        this.pmOpen = pmOpen;
    }

    public LocalTime getPmClose() {
        return pmClose;
    }

    public void setPmClose(LocalTime pmClose) {
        this.pmClose = pmClose;
    }

    public ServiceTimeType getServiceTimeType() {
        return serviceTimeType;
    }

    public void setServiceTimeType(ServiceTimeType serviceTimeType) {
        this.serviceTimeType = serviceTimeType;
    }

    @Override
    public String toString() {
        return "ServiceTime{" +
                "serviceType=" + serviceType +
                ", enable=" + enable +
                ", amOpen=" + amOpen +
                ", amClose=" + amClose +
                ", pmOpen=" + pmOpen +
                ", pmClose=" + pmClose +
                ", serviceTimeType=" + serviceTimeType +
                '}';
    }
}
